跳到主要内容

OpenTrans

Github 链接:https://github.com/RainStorm108/OpenTrans

OpenTrans 是一款使用 Ollama 的私有本地 LLM 批量翻译工具。它旨在将源目录镜像到目标语言,同时保持精确的文件结构。

主要功能

  • 本地 (Ollama): 使用 Gemma、Llama 3 或 DeepSeek 等模型进行私有、无成本的翻译。
  • 语法保护: 自动保护代码块 (```), inline code (`), LaTeX math ($), and Markdown links from being corrupted by the LLM.
  • Directory Mirroring: Recursively replicates your source folder structure in the output destination.
  • Parallel Processing: Uses ThreadPoolExecutor for high-speed batch handling of large file sets.
  • Smart Caching: Uses hashing to track file changes. Only files that have been modified since the last run are sent to the LLM, saving significant time and compute resources.
  • Resilient File Handling: Automatically pulls required models from Ollama with a real-time progress bar if they are missing.

Quick Usage

Simply provide the input directory and the target output directory.

opentrans ./docs ./translated_docs --config ./config.yaml`

Example Output:

目标语言:中文
输入:`/home/user/Projects/OpenTrans/docs`
输出:`/home/user/Projects/OpenTrans/translated_docs`
使用模型:translategemma:4b

翻译文件:100%|████████████████████████████████| 12/12 [00:45<00:00, 3.7s/file]

完成。

Running Examples

# 翻译 Docusaurus
`opentrans ./Example/Docusaurus/docs ./Example/Docusaurus/i18n/zh-hans/docusaurus-plugin-content-blog/current ./config.yaml`

Installation

Setup

OpenTrans requires Ollama to be installed and running on your local machine.

  1. Install Ollama: Follow instructions at Ollama

  2. Install uv

`curl -LsSf https://astral.sh/uv/install.sh | sh`
`uv tool install .`
  1. Install OpenTrans
`git clone https://github.com/rainstorm108/OpenTrans.git`
`cd OpenTrans`
`uv pip install .`
  1. run
`opentrans ./docs ./translated_docs --config ./config.yaml`

For Developers

  1. Environment Setup
`uv sync`
`source .venv/bin/activte`
`hatch shell`
`uv pip install -e .`
  1. Running Tests
`hatch test`

Folder Structure

OpenTrans/
├── src/
│ └── OpenTrans/
│ ├── __init__.py
│ ├── main.py # 使用 Click 的 CLI 界面
│ ├── settings.py # 翻译设置
│ ├── translator.py # 翻译逻辑
│ ├── cache_manager.py # 使用内容哈希来管理翻译缓存,以跳过未更改的文件。
│ └── hasher.py # 对文件进行哈希
├── tests/
│ ├── test_cache_manager.py
│ ├── test_hasher.py
│ └── test_translator.py
├── config.yaml # 全局设置 (模型、语言等)
├── pyproject.toml
└── README.md

Workflow

待办事项

  • 基于 Click 的 CLI 界面
  • 基于占位符的语法保护 (代码/LaTeX)
  • 多线程并行处理
  • 实现哈希缓存,跳过未更改的文件
  • 完成 Docusaurus 翻译脚本
  • 使用 Tree-sitter 替换代码块,而不是使用正则表达式
  • 支持更多文件类型
  • ...

许可证

该项目采用 MIT 许可证。